define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'partner/level/setting/index' + location.search,
                    add_url: 'partner/level/setting/add',
                    edit_url: 'partner/level/setting/edit',
                    del_url: 'partner/level/setting/del',
                    multi_url: 'partner/level/setting/multi',
                    import_url: 'partner/level/setting/import',
                    table: 'partner_level_setting',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'level', title: __('Level'), searchList: {"1":__('Level 1'),"2":__('Level 2'),"3":__('Level 3'),"4":__('Level 4'),"5":__('Level 5')}, formatter: Table.api.formatter.normal},
                        {field: 'level_name', title: __('Level_name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'upgrade_button',
                                    text: '升级条件设置',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-check-square',
                                    url: function (row) {
                                        return Fast.api.fixurl('partner/upgrade/setting?level.level_name=' + row.level_name)
                                    },
                                    extend: "data-area='[\"80%\", \"80%\"]'",
                                },
                            ],
                            formatter: function(value, row, index){
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.level == 3){ //这里的3就是当前的第一级，5级改3级了
                                    $(table).data("operate-upgrade_button", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
